<template>
	<mescroll-uni :ref="'mescrollRef'+i" @init="mescrollInit" bottom="100" height="100%" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback">
		<view class="data-list" v-for="(item, key) in dataList" :key="key" @click="toInfo(item)">
			<view class="data-item">
				<view class="item-text item-title">
					{{item.subject}}
				</view>
				<view class="item-text item-desc">
					当前节点：
					<template v-if="index === 0">
						{{item.name}}
					</template>
					<template v-else>
						{{item.curNode}}
					</template>
				</view>
				<view class="item-text item-desc">
					待办接收时间：{{item.createTime}}
				</view>
			</view>
		</view>
	</mescroll-uni>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js"
	import MescrollMoreItemMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js";
	import { getPendingTasks, getHandledTasks, getInitiateTasks, getFormData, getFormInfo } from '@/api/base/flow.js'
	import { tt as constants } from '@/utils/business/constants.js'
	
	export default {
		mixins: [MescrollMixin,MescrollMoreItemMixin],
		props:{
			i: Number,
			index: { 
				type: Number,
				default(){
					return 0
				}
			},
			params: {
				type: Object,
				default(){
					return {}
				}
			},
			searchText: {
				type: String,
				default(){
					return ""
				}
			}
		},
		data() {
			return {
				downOption:{
					auto:false
				},
				upOption:{
					auto:false
				},
				dataList: [] ,
				queryParams: [],
			}
		},
		methods: {
			init() {
				this.queryParams = []
				// 搜索
				if(this.searchText){
					this.queryParams.push({
						key: "Q^temp.subject_^SL",
						value: this.searchText
					})
				}
				// 筛选
				if(this.params.processName || this.params.processKey){
					if(this.index == 0){
						this.queryParams.push({
							key: "Q^temp.proc_def_key_^S",
							value: this.params.processKey
						})
					}else if(this.index == 1){
						this.queryParams.push({
							key: "Q^inst.proc_def_name_^SL",
							value: this.params.processName
						})
					}else if(this.index == 2){
						this.queryParams.push({
							key: "Q^proc_def_name_^SL",
							value: this.params.processName
						})
					}
				}
				if(this.params.startTime){
					if(this.index == 0){
						this.queryParams.push({
							key: "Q^temp.create_time_^DL",
							value: this.params.startTime
						})
					}else if(this.index == 1){
						this.queryParams.push({
							key: "Q^inst.create_time_^DL",
							value: this.params.startTime
						})
					}else if(this.index == 2){
						this.queryParams.push({
							key: "Q^create_time_^DL",
							value: this.params.startTime
						})
					}
				}
				if(this.params.endTime){
					if(this.index == 0){
						this.queryParams.push({
							key: "Q^temp.create_time_^DG",
							value: this.params.endTime
						})
					}else if(this.index == 1){
						this.queryParams.push({
							key: "Q^inst.create_time_^DG",
							value: this.params.endTime
						})
					}else if(this.index == 2){
						this.queryParams.push({
							key: "Q^create_time_^DG",
							value: this.params.endTime
						})
					}
				}
			},
			downCallback() {
				this.dataList = []
				this.mescroll.resetUpScroll()
			},
			upCallback(page) {
				let pageNum = page.num // 页码, 默认从1开始
				let pageSize = page.size // 页长, 默认每页10条
				let pager = {
					start: page.num,
					limit: page.size
				}
				
				let funcName
				if(this.index == 0){
					funcName = getPendingTasks
				}else if(this.index == 1){
					funcName = getHandledTasks
				}else if(this.index == 2){
					funcName = getInitiateTasks
				}
				
				this.init()
				
				funcName(pager, this.queryParams).then(res =>{
					let curPageData = res.data.dataResult
					let curPageLen = curPageData.length
					let totalSize = curPageLen == 0 ? 0 : res.data.pageResult.totalCount
					if(page.num == 1){
						//如果是第一页需手动置空列表
						this.dataList = []
					}
					
					// 工作票主票流程
					let wtFlows = [...constants.WTFlows, ...constants.WTFlowsNew, ...constants.WTFlowsFire]
					// 工作票附票流程
					let atFlows = [...constants.ATFlows, ...constants.ATFlowsNew, ...constants.ATFlowsFire]
					// 工作票流程
					let watFlows = [...wtFlows, ...atFlows]
					// 操作票流程
					let otFlows = [...constants.OTFlow, ...constants.OTFlowNew, ...constants.OTFlowFire]
					
					let workFlows = []
					watFlows.forEach(item => {
						workFlows.push(item.value)
					})
					let operateFlows = []
					otFlows.forEach(item => {
						operateFlows.push(item.value)
					})
					
					// 处理结束流程状态显示
					curPageData.forEach(item=>{
						if(item.status == 'end') {
							if(operateFlows.join().includes(item.procDefKey)) { // 操作票
								item.curNode = '已执行'
							}
							if(workFlows.join().includes(item.procDefKey)) { // 工作票
								item.curNode = '已终结'
							}
						}
					})
					
					this.dataList = this.dataList.concat(curPageData) //追加新数据
					this.mescroll.endBySize(curPageLen, totalSize) 
				}).catch(()=>{
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			async toInfo(item) {
				let url
				if(this.index === 0) {
					await getFormData(item.taskId).then(res=>{
						url = res.data.formModel.mobileValue
					})
					if(url.indexOf('type') === -1) {
						uni.navigateTo({
							url: url + "&type=approve&taskId=" + item.taskId + "&nodeId=" + item.nodeId + "&procDefKey=" + item.procDefKey
						})
					} else {
						uni.navigateTo({
							url: url + "&taskId=" + item.taskId + "&nodeId=" + item.nodeId + "&procDefKey=" + item.procDefKey
						})
					}
				} else {
					await getFormInfo(item.id).then(res=>{
						url = res.data.formModel.mobileValue
					})
					uni.navigateTo({
						url: url + "&type=view"
					})
				}
			},
			reload(){
				this.dataList = []
				this.mescroll.resetUpScroll()
			}
		}
	}
</script>

<style lang="scss">
	@import '@/static/style/common-list.scss';
</style>